
import gov.nih.mipav.view.ViewUserInterface;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


public class ChartManager implements ActionListener {

	private HashMap<String, ImageChart> imageChartMap;

	public static final double MIN_Y_VALUE = 0.05;
	public static final double GAP = 0.1;
	private ArrayList<Region> regions;

	private static ChartManager instance;

	private ChartManager() {
		imageChartMap = new HashMap<String, ImageChart>();
	}

	public static ChartManager getInstance() {
		if (instance == null)
			instance = new ChartManager();
		return instance;
	}

	public void setData(int numberOfImages, int numberOfRegions, double[] mean,
			double[] variance, double[] curtosis) {
		imageChartMap.clear();

		ImageManager imageManager = DataManager.getInstance().getImageManager();
		ArrayList<String> names = imageManager.getImagesNames();

		for (int j = 0; j < numberOfImages; j++) {

			ImageChart chart = new ImageChart(names.get(j));

			RegionManager regionManager = RegionManager.getInstance();
			regions = regionManager.getRegions();
			for (int i = 0; i < numberOfRegions - 1; i++) {

				regions.get(i).setMedia(mean[j + i * numberOfImages]);
				regions.get(i).setVariance(variance[j + i * numberOfImages]);
				regions.get(i).setCurtosis(curtosis[j + i * numberOfImages]);

				chart.addRegionData(regions.get(i).getName(), mean[j + i
						* numberOfImages], Math.sqrt(variance[j + i
						* numberOfImages]));

			}

			imageChartMap.put(chart.getName(), chart);
		}
	}

	public void showChart() {

		JTabbedPane tabbedPane = new JTabbedPane();

		Collection<String> images = imageChartMap.keySet();
		for (Iterator<String> iterator = images.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			JComponent panel = new JPanel();
			tabbedPane.addTab(name, panel);
			panel.setLayout(new BorderLayout());
			panel.add(imageChartMap.get(name).getChartPanel(),
					BorderLayout.CENTER);

			JPanel dataPanel = new JPanel();
			dataPanel.setLayout(new FlowLayout());
			panel.add(dataPanel, BorderLayout.NORTH);

			JButton createHTMLButton = new JButton("Data");
			createHTMLButton.setActionCommand("HTML");
			createHTMLButton.addActionListener(this);
			dataPanel.add(createHTMLButton);
		}

		ViewUserInterface userInterface = ViewUserInterface.getReference();

		JDialog charts = new JDialog(userInterface.getMainFrame());
		charts.getContentPane().add(tabbedPane);
		charts.pack();
		charts.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();

		if (command.equals("HTML")) {
			JSHTMLCreator creator = new JSHTMLCreator();
			creator.create(regions);
		}
	}
}
